<?php
namespace app\api\controller;
use app\api\model\Goods as GoodsModel;
use app\api\model\Cart as CartModel;
use app\api\model\Category as CategoryModel;

/**
 * 菜品控制器
 */
class Goods extends Controller
{
    /**
     * 菜品列表
     */
    public function lists($food_mode=0)
    {
		$user = $this->getUser();
        $model = new GoodsModel;
		$category = array_values(CategoryModel::getCacheTree($this->shop_id));
		$result = $model->getGoodsList($this->shop_id,$category,$user,$food_mode);
		$goods = $result['list'];
		$order_total_num = $result['cartlist']['order_total_num'];
		$order_total_price = $result['cartlist']['order_total_price'];
		$min_price = $result['cartlist']['min_price'];
		return $this->renderSuccess(compact('category','goods','order_total_num','order_total_price','min_price'));
    }

    /**
     * 获取商品详情
     */
    public function detail($goods_id)
    {
        // 商品详情
        $detail = GoodsModel::detail($goods_id);
        if (!$detail || $detail['goods_status']['value'] != 10) {
            return $this->renderError('很抱歉，商品信息不存在或已下架');
        }
        // 规格信息
        $specData = $detail['spec_type'] == 20 ? $detail->getManySpecData($detail['spec_rel'], $detail['spec']) : null;
//        $user = $this->getUser();
//        // 购物车商品总数量
//        $cart_total_num = (new CartModel($user['user_id']))->getTotalNum();
        return $this->renderSuccess(compact('detail', /*'cart_total_num',*/ 'specData'));
    }

}
